package com.abel.bigwater.dataserver.model

import com.abel.bigwater.model.JsonHelper
import com.alibaba.fastjson.annotation.JSONField
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.index.CompoundIndexes
import org.springframework.data.mongodb.core.index.IndexDirection
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
import java.util.*

@Document("rtuDevice")
@CompoundIndexes(
        CompoundIndex(def = "{'firmId':1,'rtuId':1}", unique = true)
)
class HgdaoDevice : Cloneable {
    /**
     * @param rtuId the rtuId to set
     */
    @Indexed(unique = true)
    var rtuId: String? = null

    var firmId: String? = null

    /**
     * 设备配置的口令
     */
    var passwd: String? = "666666"

    /**
     * @param pulse the pulse to set
     */
    var pulse: Short? = 1.toShort()
        set(pulse) {
            if (pulse ?: 0 < 1) {
                field = 1
            }
            field = pulse
        }

    /**
     * 底度
     */
    var baseAll = 0

    /**
     * 正向行度通道, 用于STA
     */
    var forwardChannel: Short? = 1

    /**
     * 反向行度通道, 用于STA
     */
    var revertChannel: Short? = 2

    /**
     * @param analogRange the analogRange to set
     */
    var analogRange: Double? = null

    /**
     * pressure1 = pressueB + pressureReading * pressureK
     */
    var pressureChannel: Short? = null

    /**
     * This is for h86. default to 0.01.
     * @param pressureK the pressureK to set
     */
    var pressureK: Double = 0.01

    /**
     * default to 0.0
     * @param pressureB the pressureB to set
     */
    var pressureB: Double = 0.0

    /**
     * This is for realtime.
     */
    var textK: Double = 1.0

    /**
     * This is for hgdao-udp.
     */
    var abcdK: Double = 100.0

    /**
     * one RTU may sample more than 1 stations' data. such as for both
     * residential and fire. Generally [.rtuName] is for residential
     * or office.
     */
    var rtuName: String? = null

    /**
     * 正向行度通道, 用于STA
     */
    var forwardChannel2: Short? = null

    /**
     * 反向行度通道, 用于STA
     */
    var revertChannel2: Short? = null

    /**
     * one RTU may sample more than 1 stations' data. such as for both
     * residential and fire. Generally [.rtuName2] is for fire.
     */
    var rtuName2: String? = null

    /**
     * to identify the message.
     */
    @Indexed(direction = IndexDirection.DESCENDING)
    @JSONField(format = JsonHelper.FULL_DATE_FORMAT)
    var createTime: Date? = null

    /**
     * to identify the message.
     */
    @Indexed(direction = IndexDirection.DESCENDING)
    @JSONField(format = JsonHelper.FULL_DATE_FORMAT)
    var updateTime: Date? = null

    /**
     * hz meter
     */
    var hzUrl = "http://58.211.253.180:8000/web/iinfop"

    /**
     * @param hzName the hzName to set
     */
    var hzName: String? = null

    /**
     * @param hzHh the hzHh to set
     */
    var hzHh: String? = null

    /**
     * @param hzMn the hzMn to set
     */
    var hzMn: String? = null

    override public fun clone(): HgdaoDevice {
        return super.clone() as HgdaoDevice
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    override fun toString(): String {
        return String.format(
                "{rtuId:%s, pulse:%s, baseAll:%s, analogRange:%s, pressureChannel:%s, pressureK:%s, pressureB:%s, hzUrl:%s, hzName:%s, hzHh:%s, hzMn:%s}",
                rtuId, this.pulse, baseAll, this.analogRange, pressureChannel, this.pressureK, this.pressureB, hzUrl, hzName, hzHh, hzMn)
    }

    companion object {
        const val COL_RTU_DEVICE = "rtuDevice"
        const val PREK_DEFAULT = 0.01
        const val PREB_DEFAULT = 0.0
        const val ABCDK_DEFAULT = 100.0
        const val TEXTK_DEFAULT = 1.0

        val DEMO_RTU_DEVICE = HgdaoDevice().apply {
            forwardChannel = 1
            revertChannel = 2
            pressureChannel = 5

            pulse = 1000

        }
    }
}
